package com.sgchen.security.register;

import com.sgchen.security.config.SecretConfig;
import com.sgchen.security.rsa.controller.AsymmetricEncryptionController;
import com.sgchen.security.rsa.service.AsymmetricEncryptionKeyManageService;
import com.sgchen.security.rsa.service.AsymmetricEncryptionSessionService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class SecretConfigPostProcessor implements BeanPostProcessor {

    private final ApplicationContext applicationContext;

    public SecretConfigPostProcessor(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof SecretConfig) {
            SecretConfig secretConfig = (SecretConfig) bean;
            // 检查 RequestAsymmetricMode 属性是否不为空
            if (secretConfig.getRequestAsymmetricMode() != null) {
                // 将 ApplicationContext 转换为 ConfigurableApplicationContext
                ConfigurableApplicationContext configurableContext =
                        (ConfigurableApplicationContext) applicationContext;

                // 获取 BeanFactory
                AutowireCapableBeanFactory beanFactory = configurableContext.getBeanFactory();

                // 创建并初始化 Bean
                AsymmetricEncryptionKeyManageService keyManageService = new AsymmetricEncryptionKeyManageService();
                beanFactory.autowireBean(keyManageService);
                beanFactory.initializeBean(keyManageService, "asymmetricEncryptionKeyManageService");
                // 注册为单例 Bean
                configurableContext.getBeanFactory().registerSingleton("asymmetricEncryptionKeyManageService", keyManageService);

                AsymmetricEncryptionSessionService sessionService = new AsymmetricEncryptionSessionService();
                beanFactory.autowireBean(sessionService);
                beanFactory.initializeBean(sessionService, "asymmetricEncryptionSessionService");
                // 注册为单例 Bean
                configurableContext.getBeanFactory().registerSingleton("asymmetricEncryptionSessionService", sessionService);

                registerController();
            }
        }
        return bean;
    }

    private void registerController() {
        // 获取 BeanFactory
        DefaultListableBeanFactory beanFactory =
                (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory();

        // 注册 AsymmetricEncryptionController Bean
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(
                AsymmetricEncryptionController.class
        );
        beanFactory.registerBeanDefinition("asymmetricEncryptionController", builder.getBeanDefinition());
    }

}